<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Client</title>
</head>
<body>
    <h1 id="title">WebSocket Client</h1>
    <script>
        var wsServer = 'ws://127.0.0.1:9502';
        var websocket = new WebSocket(wsServer);
        websocket.onopen = function (evt) {
            websocket.send( 'hello-ws' ); // 向ws 发送消息
            console.log("Connected to WebSocket server.");
        };

        websocket.onclose = function (evt) {
            console.log("Disconnected");
        };

        websocket.onmessage = function (evt) {
            var d1 = document.getElementById('title');
            d1.insertAdjacentHTML('afterend', '<p>server_msg:&nbsp;' + evt.data + '</p>');
            console.log('Retrieved data from server: ' + evt.data);
        };

        websocket.onerror = function (evt, e) {
            console.log('Error occured: ' + evt.data);
        };
    </script>
</body>
</html>